package com.wolves.career.controller;


import com.wolves.career.entity.JobOpportunityRecruitment;
import com.wolves.career.entity.vo.RecruitmentVo;
import com.wolves.career.entity.vo.RescruitmentDes;
import com.wolves.career.mapper.JobOpportunityRecruitmentMapper;
import com.wolves.career.service.JobOpportunityRecruitmentService;
import com.wolves.career.utils.R;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-23
 */
@RestController
@RequestMapping("/job/recruitment")
@CrossOrigin
public class JobOpportunityRecruitmentController {

    @Autowired
    JobOpportunityRecruitmentService recruitmentService;

    /**
     * 添加招聘信息
     *
     * @param recruitment 招聘信息类
     * @return
     */
    @PostMapping("/addRecruitment")
    public R addRecruitment(@RequestBody(required = true) JobOpportunityRecruitment recruitment) {
        boolean save = recruitmentService.save(recruitment);
        if (!save) {
            return R.error().message("添加失败").data("success", false);
        }
        return R.ok().data("success", true);

    }

    /**
     * 删除招聘信息
     *
     * @return
     */
    @DeleteMapping("/delete/{recruitmentId}")
    public R removeRecruitment(@PathVariable String recruitmentId) {
        boolean remove = recruitmentService.removeById(recruitmentId);
        if (!remove) {
            return R.error().message("插入失败").data("success", false);
        }
        return R.ok().data("success", true);
    }

    public R updateRecruitment(@RequestBody(required = true)
                                       JobOpportunityRecruitment recruitment) {
        boolean update = recruitmentService.updateById(recruitment);
        if (!update) {
            return R.error().message("更新失败").data("success", false);
        }
        return R.ok().data("success", true);

    }

    /**
     * 【主要业务】
     * 根据职位或者公司名称返回招聘信息封装
     *
     * @param rescruitmentDes 公司或者职位名称的封装类 模糊查询
     * @param pageNo
     * @param limit
     * @return 招聘信息封装类
     */
    @ApiOperation("查询卡片")
    @PostMapping("/getRecruitmentBy/{pageNo}/{limit}")
    public R getRecruitment(@RequestBody(required = false) RescruitmentDes rescruitmentDes,
                            @PathVariable int pageNo,
                            @PathVariable int limit) {
        //获取查询信息
        String des = rescruitmentDes.getDes();
        String positionId = rescruitmentDes.getPositionId();
        String technologyId = rescruitmentDes.getTechnologyId();
        List<RecruitmentVo> list;
        if (StringUtils.isEmpty(des) && StringUtils.isEmpty(positionId) && StringUtils.isEmpty(technologyId)) {
            //如果没有封装信息就返回所有
            list = recruitmentService.getAllRecruitment();
        } else {
            if (!StringUtils.isEmpty(des)) {
                //如果有公司信息就模糊查询公司信息
                list = recruitmentService.getRecruitmentByCompanyOrPosition(des);
            }else if (!StringUtils.isEmpty(positionId)){
                //如果有职位id就查询职位
                return getRecruitmentByPositionId2(pageNo,limit,positionId);
            }else {
                //如果有技术id就查询职位id
                return getRecruitmentByTechnology(technologyId,pageNo,limit);
            }
        }
        if (list.size() == 0) {
            //如果数量为0返回null
            return R.ok().data("records", null).data("total", list.size());
        }
        //获起始位置
        int begin = (pageNo - 1) * limit;
        if (pageNo * limit > list.size()) {
            //判断是否为最后一组（最后一组数据可能不满limit）
            //少了多少个元素
            int less = pageNo * limit - list.size();
            //减去
            limit = limit - less;
        }
        //获取起始
        int end = begin + limit;
        //当起始页超过了list.size()就返回null
        if (begin > list.size()) {
            return R.ok().data("records", null).data("total", list.size());
        }
        //获取分页片段
        List<RecruitmentVo> records = list.subList(begin, end);
        //根据热度降序排序
        records.sort((x, y) -> Integer.compare(y.getHeat(), x.getHeat()));

        return R.ok().data("records", records).data("total", list.size());

    }

    /**
     * 获取热门的招聘类
     *
     * @param limit
     * @return
     */
    @GetMapping("/getHot/{limit}")
    public R getHotRecruitment(@PathVariable Integer limit) {

        List<RecruitmentVo> list = recruitmentService.getAllRecruitment();
        if (list == null) {
            return R.ok().data("records", null);
        }
        if (limit > list.size()) {
            //如果需要的数量大于list的数量就只能返回list的数量
            limit = list.size();
        }
        list.sort((x, y) -> Integer.compare(y.getHeat(), x.getHeat()));
        List<RecruitmentVo> hotList = list.subList(0, limit);
        return R.ok().data("records", hotList);

    }

    /**
     * 根据职位id获取招聘卡片
     */
    @Autowired
    JobOpportunityRecruitmentMapper jobOpportunityRecruitmentMapper;

    @ApiOperation(value = "通过职位id查询招聘信息（卡片）")
    @GetMapping("/getByPositionId/{pageNo}/{limit}/{positionId}")
    public R getRecruitmentByPosition(@PathVariable String positionId, @PathVariable int pageNo, @PathVariable int limit) {
        List<RecruitmentVo> finalList = new ArrayList<>();
        //根据职位id获取所有招聘信息
        List<RecruitmentVo> list = jobOpportunityRecruitmentMapper.getRecruitmentByPositionId(positionId);
        if (list.size() == 0) {
            return R.ok().message("该职位没有职位");
        }
        for (RecruitmentVo recruitmentVo : list) {
            String positionName = recruitmentVo.getPositionName();
            List<RecruitmentVo> oneList = recruitmentService.getRecruitmentByCompanyOrPosition(positionName);
            finalList.addAll(oneList);
        }
        //分页

        //获起始位置
        int begin = (pageNo - 1) * limit;
        if (pageNo * limit > finalList.size()) {
            //判断是否为最后一组（最后一组数据可能不满limit）
            //少了多少个元素
            int less = pageNo * limit - finalList.size();
            //减去
            limit = limit - less;
        }
        //获取起始
        int end = begin + limit;
        //当起始页超过了list.size()就返回null
        if (begin > finalList.size()) {
            return R.ok().data("records", "no data").data("total", finalList.size());
        }
        //获取分页片段
        List<RecruitmentVo> records = finalList.subList(begin, end);
        //根据热度降序排序
        records.sort((x, y) -> Integer.compare(y.getHeat(), x.getHeat()));


        return R.ok().data("records", records).data("total", finalList.size());
    }


    @GetMapping("/get/{pageNo}/{limit}/{positionId}")
    public R getRecruitmentByPositionId2(@PathVariable int pageNo, @PathVariable int limit, @PathVariable String positionId) {
        List<RecruitmentVo> list = recruitmentService.getAllRecruitment();
        List<RecruitmentVo> finalList = new ArrayList<>();
        for (RecruitmentVo recruitmentVo : list) {
            String positionId1 = recruitmentVo.getPositionId();
            if (positionId.equals(positionId1)) {
                finalList.add(recruitmentVo);
            }
        }

        //分页


        //获起始位置
        int begin = (pageNo - 1) * limit;
        if (pageNo * limit > finalList.size()) {
            //判断是否为最后一组（最后一组数据可能不满limit）
            //少了多少个元素
            int less = pageNo * limit - finalList.size();
            //减去
            limit = limit - less;
        }
        //获取起始
        int end = begin + limit;
        //当起始页超过了list.size()就返回null
        if (begin > finalList.size()) {
            return R.ok().data("records", "no data").data("total", finalList.size());
        }
        //获取分页片段
        List<RecruitmentVo> records = finalList.subList(begin, end);
        //根据热度降序排序
        records.sort((x, y) -> Integer.compare(y.getHeat(), x.getHeat()));


        return R.ok().data("records", records).data("total", finalList.size());


    }


    /**
     * 暂时不使用该方法
     *
     * @param technologyId
     * @return
     */
    @ApiOperation(value = "通过技术id查找招聘信息（卡片）")
    @GetMapping("/getByTechnologyId/{pageNo}/{limit}/{technologyId}")
    public R getRecruitmentByTechnology(@PathVariable String technologyId, @PathVariable int pageNo, @PathVariable int limit) {
        List<RecruitmentVo> finalList = new ArrayList<>();
        List<RecruitmentVo> recruitmentVoList = recruitmentService.getAllRecruitment();
        //通过遍历找到对应的
        for (RecruitmentVo recruitmentVo : recruitmentVoList) {
            HashMap<String, String> technologyMap = recruitmentVo.getTechnologyMap();
            for (String s : technologyMap.keySet()) {
                if (s.equals(technologyId)) {
                    finalList.add(recruitmentVo);
                }
            }
        }

        //分页

        //获起始位置
        int begin = (pageNo - 1) * limit;
        if (pageNo * limit > finalList.size()) {
            //判断是否为最后一组（最后一组数据可能不满limit）
            //少了多少个元素
            int less = pageNo * limit - finalList.size();
            //减去
            limit = limit - less;
        }
        //获取起始
        int end = begin + limit;
        //当起始页超过了list.size()就返回null
        if (begin > finalList.size()) {
            return R.ok().data("records", null).data("total", finalList.size());
        }
        //获取分页片段
        List<RecruitmentVo> records = finalList.subList(begin, end);
        //根据热度降序排序
        records.sort((x, y) -> Integer.compare(y.getHeat(), x.getHeat()));


        return R.ok().data("records", records).data("total", finalList.size());

    }


}

